package com.slz3.business.module.drawManagement.draGoods.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.drawManagement.draGoods.domain.DraGoods;
import com.slz3.business.module.drawManagement.draGoods.domain.DraGoodsFactory;
import com.slz3.business.module.drawManagement.draGoods.steward.DraGoodsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class DraGoodsServiceImpl implements DraGoodsService {

    @Override
    public DraGoods save(DraGoods draGoods) {
        return Repositorys.draGoods.save(DraGoodsFactory.initBaseFields(draGoods));
    }

    @Override
    public Page<DraGoods> page(MultiValueMap<String, String> params) {
        Page<DraGoods> draGoodsPage = Repositorys.draGoods.findAll(
                DraGoodsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return draGoodsPage;
    }

    @Override
    public DraGoods modify(DraGoods draGoodsNew) {
        return Repositorys.draGoods.saveAndFlush(DraGoodsFactory.initBaseFields(draGoodsNew));
    }

    @Override
    public Optional<DraGoods> single(UUID uuid) {
        return Repositorys.draGoods.findById(uuid);
    }

    @Override
    public List<DraGoods> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.draGoods.findAllById(uuids);
    }

    @Override
    public List<DraGoods> delete(List<DraGoods> draGoodsList) {
        draGoodsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            DraGoodsFactory.initBaseFields(item);
        });
        return Services.draGoodsTransactional.batchModify(draGoodsList);
    }

}
